package org.lex.net.event;

import javax.swing.event.EventListenerList;

public class ProtocolEventSupport {
	private EventListenerList listenerList = new EventListenerList();
	private final Object source;

	public ProtocolEventSupport(Object source) {
		this.source = source;
	}

	public void addProtocolListener(ProtocolListener l) {
		this.listenerList.add(ProtocolListener.class, l);
	}

	public void removeProtocolListener(ProtocolListener l) {
		this.listenerList.remove(ProtocolListener.class, l);
	}

	public void fireCommandSent(String command, String message) {
		ProtocolEvent e = new ProtocolEvent(source, command, message);
		Object[] listeners = listenerList.getListenerList();
		for (int i = listeners.length - 2; i >= 0; i -= 2)
			if (listeners[i] == ProtocolListener.class)
				((ProtocolListener) listeners[i + 1]).commandSent(e);
	}

	public void fireReplyRecived(int replyCode, String message) {
		ProtocolEvent e = new ProtocolEvent(source, replyCode, message);
		Object[] listeners = listenerList.getListenerList();
		for (int i = listeners.length - 2; i >= 0; i -= 2)
			if (listeners[i] == ProtocolListener.class)
				((ProtocolListener) listeners[i + 1]).replyReceived(e);
	}
}
